Skip to content

feat: add MessageLoader for loading translation scripts from JSON files#13

Merged
ramsey merged 1 commit intomainfrom
feature/message-loader
Nov 15, 2021
Merged

feat: add MessageLoader for loading translation scripts from JSON files#13
ramsey merged 1 commit intomainfrom
feature/message-loader

Conversation

@ramsey
Copy link
Copy Markdown
Contributor

@ramsey ramsey commented Nov 9, 2021

Description

This provides a MessageLoader for loading translation strings from JSON files.

For example (I'm working on examples to put into the README, btw):

use FormatPHP\FormatPHP;
use FormatPHP\Intl\Locale;
use FormatPHP\MessageLoader;
use FormatPHP\Reader\Format\Smartling as SmartlingFormatReader;

$userLocale = new Locale('fr-CA');
$defaultLocale = new Locale('en');
$config = new Config($userLocale, $defaultLocale);

$loader = new MessageLoader('/path/to/locales', $config, new SmartlingFormatReader());

$frenchMessages = $loader->loadMessages();

// We can then use the messages with FormatPHP.
$formatphp = new FormatPHP($config, $frenchMessages);

echo $formatphp->formatMessage([
    'id' => 'some.identifier',
    'defaultMessage' => 'A default message that gets used if some.identifier does not exist.',
]);

This will look for JSON files in /path/to/locales. It will first look for fr-CA.json. If it can't find that, it looks for fr.json. Finally, if it can't find that, it uses en.json (as the default locale).

Product requirements and context

How has this been tested?

PR Checklist

  • I have added tests to cover my changes.

@ramsey ramsey requested review from a team, jmauerhan and xiian November 9, 2021 18:34
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
private function validateShape($messageId, $message): void
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
*/
private function validateShape($messageId, $message): void
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

Comment thread src/MessageLoader.php
* @throws InvalidArgumentException
* @throws LocaleNotFoundException
*/
private function getLocaleMessages(): array
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function getLocaleMessages has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.


unset($data['smartling']);

foreach ($data as $messageId => $message) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

{
$messages = new MessageCollection($config);

foreach ($data as $messageId => $message) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

@qlty-cloud-legacy
Copy link
Copy Markdown

Code Climate has analyzed commit ac8a2b1 and detected 5 issues on this pull request.

Here's the issue category breakdown:

Category Count
Complexity 1
Duplication 4

The test coverage on the diff in this pull request is 100.0% (80% is the threshold).

This pull request will bring the total coverage in the repository to 100.0% (0.0% change).

View more on Code Climate.

@ramsey ramsey merged commit a9bd712 into main Nov 15, 2021
@ramsey ramsey deleted the feature/message-loader branch November 15, 2021 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant